import { defineConfig } from 'vite'
import type { UserConfig, ConfigEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
// import vueJsx from '@vitejs/plugin-vue-jsx'; //jsx支持需要引入插件
import AutoImport from 'unplugin-auto-import/vite'; //自动导入composition api 和 生成全局typescript说明
import Components from 'unplugin-vue-components/vite'; //自动引入插件，可以省去比如 UI 库的大量 import 语句
import { ElementPlusResolver, VantResolver } from 'unplugin-vue-components/resolvers';

// import WindiCSS from 'vite-plugin-windicss';
// import legacy from '@vitejs/plugin-legacy';



import { resolve } from 'path'
// https://vitejs.dev/config/
export default ({ mode }: ConfigEnv): UserConfig => {
  return {
    // plugins: [vue()],

    resolve: {
      alias: {
        '@': resolve(__dirname, 'src')
      }
    },
    css: {
      modules: {
        localsConvention: 'camelCase', // 默认只支持驼峰，修改为同时支持横线和驼峰
      },
      preprocessorOptions: {
        scss: {
          charset: false,
        },
        less: {
          charset: false,
        },
      },
      // TODO 构建包含@charset问题 https://github.com/vitejs/vite/issues/5833
      // charset: false,
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              },
            },
          },
        ],
      },
    },
    plugins: [
      vue(),
      // vueJsx(),
      // WindiCSS(),
      // legacy({
      //   targets: ['defaults', 'not IE 11'],
      // }),

      AutoImport({
        include: [
          /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
          /\.vue$/,
          /\.vue\?vue/, // .vue
          /\.md$/, // .md
        ],
        dts: false,
        imports: ['vue', 'vue-router', {
          './src/utils/env': ['apiBase'],
        }],
      }),
      Components({
        dts: false,
        resolvers: [ElementPlusResolver(), VantResolver()],  //按需引入
      }),
    ],
  }
}
